package algorithm.poj.p2000;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLDecoder;

/**
 * 分析：
 * 
 * 实现：
 * 
 * 经验和教训：
 * 问题很简单，但是处理逻辑比较复杂，需细心。
 * 出过一次RE，<repeat>改成long就AC了，明明题目中有1 <= < repeat> <= 100，不知道为什么。
 * 
 * @author wong.tong@gmail.com
 *
 */
public class P2632 {

	public static void main(String[] args) throws Exception {

		InputStream input = null;
		if (false) {
			input = System.in;
		} else {
			URL url = P2632.class.getResource("P2632.txt");
			File file = new File(URLDecoder.decode(url.getPath(), "UTF-8"));
			input = new FileInputStream(file);
		}
		
		BufferedReader stdin = new BufferedReader(new InputStreamReader(input));
		String line = stdin.readLine().trim();
		int K = Integer.valueOf(line);
		for (int i = 0; i < K; i ++) {
			try {
				String[] tmp = null;
				line = stdin.readLine();
				tmp = line.split("\\s+");
				int A = Integer.valueOf(tmp[0]);
				int B = Integer.valueOf(tmp[1]);
				
				line = stdin.readLine();
				tmp = line.split("\\s+");
				int N = Integer.valueOf(tmp[0]);
				int M = Integer.valueOf(tmp[1]);
				//get robots
				Robot[] robots = new Robot[N];
				for (int j = 0; j < N; j ++) {
					line = stdin.readLine();
					tmp = line.split("\\s+");
					int x = Integer.valueOf(tmp[0]);
					int y = Integer.valueOf(tmp[1]);
					char d = tmp[2].charAt(0);
					robots[j] = new Robot(x, y, d);
				}
				
				for (int j = 0; j < M; j ++) {
					try {
						action(stdin, A, B, N, robots);
					} catch (RobotException e) {
						for (int k = j+1; k < M; k ++) {
							line = stdin.readLine();
						}
						throw e;
					}
				}
				System.out.println("OK");
			} catch (RobotException e) {
				System.out.println(e.getMessage());
			}
		}
	}

	private static void action(BufferedReader stdin, int A, int B, int N,
			Robot[] robots) throws IOException {
		
		String line;
		String[] tmp;
		line = stdin.readLine();
		tmp = line.split("\\s+");
		int index = Integer.valueOf(tmp[0]);
		char action = tmp[1].charAt(0);
		long times = Long.valueOf(tmp[2]);
		
		Robot robot = robots[index-1];
		if (action == 'L') {
			robot.left(times);
		} else if (action == 'R') {
			robot.right(times);
		} else {
			//check before go
			if (robot.direction == 0) {			//'N'
				int t = -1;
				for (int s = 0; s < N; s ++) {
					if (s != index-1 && robots[s].x == robot.x && robots[s].y > robot.y) {
						if (t == -1 || robots[s].y < robots[t].y) {
							t = s;
						}
					}
				}
				if (t != -1 && robots[t].y - robot.y <= times) {
					throw new RobotException("Robot " + index + " crashes into robot " + (t+1));
				} else if (robot.y + times > B) {
					throw new RobotException("Robot " + index + " crashes into the wall");
				} else {
					robot.forward(times);
				}
			} else if (robot.direction == 1) {	//'E'
				int t = -1;
				for (int s = 0; s < N; s ++) {
					if (s != index-1 && robots[s].y == robot.y && robots[s].x > robot.x) {
						if (t == -1 || robots[s].x < robots[t].x) {
							t = s;
						}
					}
				}
				if (t != -1 && robots[t].x - robot.x <= times) {
					throw new RobotException("Robot " + index + " crashes into robot " + (t+1));
				} else if (robot.x + times > A) {
					throw new RobotException("Robot " + index + " crashes into the wall");
				} else {
					robot.forward(times);
				}
			} else if (robot.direction == 2) {	//'S'
				int t = -1;
				for (int s = 0; s < N; s ++) {
					if (s != index-1 && robots[s].x == robot.x && robots[s].y < robot.y) {
						if (t == -1 || robots[s].y > robots[t].y) {
							t = s;
						}
					}
				}
				if (t != -1 && robot.y - robots[t].y  <= times) {
					throw new RobotException("Robot " + index + " crashes into robot " + (t+1));
				} else if (robot.y - times < 1) {
					throw new RobotException("Robot " + index + " crashes into the wall");
				} else {
					robot.forward(times);
				}
			} else if (robot.direction == 3) {	//'W'
				int t = -1;
				for (int s = 0; s < N; s ++) {
					if (s != index-1 && robots[s].y == robot.y && robots[s].x < robot.x) {
						if (t == -1 || robots[s].x > robots[t].x) {
							t = s;
						}
					}
				}
				if (t != -1 && robot.x - robots[t].x <= times) {
					throw new RobotException("Robot " + index + " crashes into robot " + (t+1));
				} else if (robot.x - times < 1) {
					throw new RobotException("Robot " + index + " crashes into the wall");
				} else {
					robot.forward(times);
				}
			}
		}
	}
	
	private static class RobotException extends RuntimeException {

		public RobotException(String string) {
			super(string);
		}

		private static final long serialVersionUID = 3390825747061749162L;
		
	}
	
	private static class Robot {
		
		int x;
		int y;
		int direction;
		static char[] directions = new char[] {'N', 'E', 'S', 'W'}; 
		
		public Robot(int x, int y, char direction) {
			this.x = x;
			this.y = y;
			for (int i = 0; i < directions.length; i ++) {
				if (directions[i] == direction) this.direction = i;
			}
		}
		
		public void forward(long steps) {
			switch (directions[this.direction]) {
			case 'N': this.y += steps; break;
			case 'E': this.x += steps; break;
			case 'S': this.y -= steps; break;
			case 'W': this.x -= steps; break;
			}
		}
		
		public void left(long times) {
			this.direction = (int)(((long)this.direction - times) & 0x3);  
		}
		
		public void right(long times) {
			this.direction = (int)(((long)this.direction + times) & 0x3);
		}
	}
}
